package cn.zymall.user.service.impl;

import cn.zymall.common.model.User;
import cn.zymall.common.utils.redis.RedisUtil;
import cn.zymall.user.mapper.SignUpMapper;
import cn.zymall.user.service.SignUpService;
import cn.zymall.user.service.UserService;
import cn.zymall.user.tool.Tools;
import cn.zymall.user.tool.Uuid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : th
 * @ClassName: SignUpImpl
 * @Description : TODO
 * @date 2020年08月19日 15:52
 **/

@Service
public class SignUpImpl implements SignUpService {


    @Autowired
    private SignUpMapper sign;


    @Autowired
    private UserService userService;


    @Autowired
    private RedisUtil redisUtil;


    /**
     * @author：th
     * @Description：发送登录注册验证码
     * @Param：value
     * @return：
     **/
    @Override
    public User send(String value){

        String regix = "^[1][3,4,5,7,8][0-9]{9}$";

        boolean flag = true;
        if (value.matches(regix)) {
            try {
                //String sms = Tools.sms(value,"SMS_200180821");
                String sms = "123456";
                flag = redisUtil.set("send"+value, sms, 300);

            }catch (Exception e){
                e.printStackTrace();
            }
                if (flag){
                                                     // 发送成功
                    return new User().setUserId(200).setUserName("Sent successfully");
                }else{                              // Redis 错误
                    return new User().setUserId(500).setUserName("Redis error");
                }
        }else{                                  // 请输入正确格式的手机号
            return new User().setUserId(500).setUserName("Please enter the correct format of mobile phone number");
        }
    }



    /**
     * @author：th
     * @Description：用户表添加数据
     * @Param：user
     * @return：boolean
     **/
    @Override
    public boolean userSave(User user){
        user.setUserName("用户_"+user.getUserPhoneNumber());
        user.setUserStatus((byte)1);
        return sign.userSave(user);
    }



    /**
     * @author：th
     * @Description：用户登录
     * @Param：用户手机号查询
     * @return：User
     **/
    @Override
    public User inquiry(String value,String sms) {

        Object o = redisUtil.get("send"+value);

        User inquiry = sign.inquiry(value);
        if (inquiry != null && sms.equals(o)){

            String uid = Uuid.uid();

            System.out.println("xx_______"+uid);


            redisUtil.set(uid,inquiry,1800);
            return new User().setUserId(inquiry.getUserId()).setUserName(uid);

        }else if (inquiry == null && sms.equals(o)){

            userSave(new User().setUserPhoneNumber(value));
            User user = userService.queryPhone(value);

            String uid = Uuid.uid();
            redisUtil.set(uid,user,1800);

            System.out.println("yy_______"+uid);


            return new User().setUserId(user.getUserId()).setUserName(uid);

        }else{                                      // 验证码输入错误
            return new User().setUserId(500).setUserName("Verification code error");
        }
    }


    /**
     * @author：th
     * @Description：用户登录
     * @Param：用户账号密码
     * @return：User
     **/
    @Override
    public User poling(String value,String pwd){

        User poling = sign.poling(value, pwd);

        if (poling != null){
            String uid = Uuid.uid();

            redisUtil.set(uid,poling,1800);
            return new User().setUserId(poling.getUserId()).setUserName(uid);
        }else{
            return new User().setUserId(500).setUserName("您输入有账号或密码不正确");
        }

    }


    /**
     * @author：th
     * @Description：发送修改密码短信
     * @Param：用户电话
     * @return：
     **/
    @Override
    public User alter(String value){
        boolean flag = true;
        try {
              String sms = "123123";
            //String sms = Tools.sms(value, "SMS_200180803");
            flag = redisUtil.set("alter"+value, sms, 300);
        }catch (Exception e){
            e.printStackTrace();
        }
            if (flag){
                //发送成功
                return new User().setUserId(200).setUserName("Sent successfully");
            }else{                                  // Redis 错误
                return new User().setUserId(500).setUserName("Redis error");
            }
    }



    /**
     * @author：th
     * @Description：用户修改登录密码
     * @Param：用户id、电话、密码、验证码
     * @return：boolean
     **/
    @Override
    public User revise(User user,String sms){
        //校验密码：只能输入6-20个字母、数字、下划线
        String regix = "/^(\\w){6,20}$/";
        if (user.getPassword().matches(regix)){
            Object o = redisUtil.get("alter"+user.getUserPhoneNumber());
            if (sms.equals(o)){
                boolean revise = sign.revise(user);
                if (revise){
                    return sign.inquiry(user.getUserPhoneNumber());
                }else{                                      // 密码修改失败
                    return new User().setUserId(500).setUserName("Password modification failed");
                }
            }else{                                          // 验证码输入错误
                return new User().setUserId(500).setUserName("Verification code error");
            }
        }else{                                            // 密码格式不正确
            return new User().setUserId(500).setUserName("Incorrect password format");
        }
    }

}
